﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using VINETLIFE.AdminCP;
using VINETLIFE.BL;
using VINETLIFE.DO;

namespace VINETLIFE.Customers
{
    public partial class TransactionHistory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CustomerId"] != null)
            {
                if (!IsPostBack)
                {
                    SEO();
                    ddlAccountTranTypeDataSource();
                    grvLogAccountDataSource();
                }
            }
            else
            {
                Functions.Alert("Bạn không có quyền truy cập vào khi chưa đăng nhập, vui lòng đăng nhập", "/dang-nhap/");
            }

        }
        private void SEO()
        {
            Page.Title = "Lịch sử giao dịch | " + Ultility.WebTitle();

            sysConfigDO objConfig = new sysConfigDO();
            objConfig.ConfigID = 2;

            objConfig = new sysConfigBL().Select(objConfig);


            HtmlMeta metaKey = new HtmlMeta();
            metaKey.Name = "keywords";
            metaKey.Content = objConfig.ConfigValue;
            Page.Header.Controls.Add(metaKey);


            objConfig.ConfigID = 3;
            objConfig = new sysConfigBL().Select(objConfig);
            HtmlMeta metaDesc = new HtmlMeta();
            metaDesc.Name = "description";
            metaDesc.Content = objConfig.ConfigValue;
            Page.Header.Controls.Add(metaDesc);
        }
        private void ddlAccountTranTypeDataSource()
        {
            ddlAccountTranType.DataSource = new cmsAccountTranMethodBL().SelectAll();
            ddlAccountTranType.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            grvLogAccountDataSource();
        }
        protected void grvLogAccountDataSource()
        {
            if (Session["CustomerId"] != null)
            {
                int customerID = int.Parse(Session["CustomerId"].ToString());
                cmsAccountDO objAccount = new cmsAccountBL().AccountInfoObject(customerID);
                if (objAccount != null)
                {
                    string type = "";
                    string logAccountPaymentCode = "";
                    DateTime searchDate = new DateTime(1900, 1, 1);
                    DateTime fromDate = new DateTime(1900, 1, 1);
                    DateTime toDate = new DateTime(1900, 1, 1);
                    logAccountPaymentCode = ddlAccountTranType.SelectedValue;

                    if (rdSearchDate.Checked)
                    {
                        type = "date";
                        if (txtSearchDate.Text != "")
                        {
                            searchDate = Myfunction.ReturnValidateDateTime(txtSearchDate.Text.Trim());
                        }
                    }
                    if (rdFromDate.Checked)
                    {
                        type = "fromdate";
                        if (txtFromDate.Text != "")
                        {
                            fromDate = Myfunction.ReturnValidateDateTime(txtFromDate.Text.Trim());
                        }
                        if (txtToDate.Text != "")
                        {
                            toDate = Myfunction.ReturnValidateDateTime(txtToDate.Text.Trim());
                        }
                    }
                    grvLogAccount.DataSource = new cmsLogAccountBL().SelectHistory(objAccount.AccountId, type, searchDate, fromDate, toDate, logAccountPaymentCode);
                    grvLogAccount.DataBind();
                }
            }
        }
        protected void grvLogAccount_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvLogAccount.PageIndex = e.NewPageIndex;
            grvLogAccountDataSource();
        }
    }
}